package context;

import service.IPaymentStrategy;
import service.impl.AlipayStrategy;

/**
 * 上下文类：订单
 * 负责使用策略，不关心具体支付方式
 */
public class Order {
    private final double amount; // 订单金额
    private IPaymentStrategy paymentStrategy; // 持有策略接口的引用

    public Order(double amount) {
        this.amount = amount;
    }

    /**
     * 动态设置支付策略
     * @param paymentStrategy 具体的支付策略对象
     */
    public void setPaymentStrategy(IPaymentStrategy paymentStrategy) {
        this.paymentStrategy = paymentStrategy;
    }

    /**
     * 执行支付：将支付行为委托给策略对象
     */
    public void executePayment() {
        if (paymentStrategy == null) {
            throw new IllegalStateException("支付策略尚未设置！");
        }
        // 委托调用 - 核心的委托机制
        paymentStrategy.pay(amount);
    }

    // 可选：提供便捷方法
    public void payWithAlipay(String account) {
        setPaymentStrategy(new AlipayStrategy(account));
        executePayment();
    }
}
